<?php

function delete_product($param) {
    mysql_query("UPDATE product SET active=false WHERE idproduct=".$param["idproduct"]);
}

$data = mysql_query("SELECT idcategory,category FROM category WHERE active=true") or die(mysql_error());
$categories = "";
while ($row = mysql_fetch_array($data)) {
    $categories .= $row["category"] . ":" . $row["category"] . ";";
}
$categories = substr($categories, 0, -1);

$col = array();
$col["title"] = "Product ID";
$col["name"] = "idproduct";
$col["width"] = "30";
$col["align"] = "center";
$cols[] = $col;

$col = array();
$col["title"] = "Product Name";
$col["name"] = "name";
$col["align"] = "center";
$col["width"] = "100";
$col["editable"] = true;
$col["editoptions"] = array("maxlength" => '100');
$col["editrules"] = array("required" => true);
$cols[] = $col;

$col = array();
$col["title"] = "Category";
$col["name"] = "category";
$col["align"] = "center";
$col["width"] = "70";
$col["editable"] = true;
$col["edittype"] = "select";
$col["editoptions"] = array("value" => $categories);
$cols[] = $col;

$col = array();
$col["title"] = "Quantity";
$col["name"] = "quantity";
$col["align"] = "center";
$col["search"] = true;
$col["width"] = "30";
$col["editable"] = true;
$col["editoptions"] = array("maxlength" => '6');
$col["editrules"] = array("number" => true, "required" => true);
$cols[] = $col;

$col = array();
$col["title"] = "Price";
$col["name"] = "price";
$col["search"] = true;
$col["width"] = "50";
$col["align"] = "center";
$col["formatter"] = "currency";
$col["editable"] = true;
$col["editoptions"] = array("maxlength" => '12');
$col["editrules"] = array("number" => true, "required" => true);
$cols[] = $col;

$col = array();
$col["title"] = "Description";
$col["name"] = "description";
$col["search"] = false;
$col["editable"] = true;
$col["width"] = "120";
$col["edittype"] = "textarea";
$col["editoptions"] = array("rows" => 4, "cols" => 15, "maxlength" => 250);
$col["align"] = "center";
$cols[] = $col;

$col = array();
$col["title"] = "Updated";
$col["name"] = "timestamp";
$col["search"] = true;
$col["width"] = "80";
$col["align"] = "center";
$cols[] = $col;

$col = array();
$col["title"] = "Product Image";
$col["name"] = "image";
$col["search"] = true;
$col["editable"] = true;
$col["edittype"] = "file";
$col["formatter"] = "image";
$col["width"] = "70";
$col["formatoptions"] = array("width" => '100', 'height' => '75');
$col["align"] = "center";
$cols[] = $col;

$g = new jqgrid();
$grid["rowNum"] = 7;
$grid["rowList"] = array(7, 14, 28);
$grid["caption"] = "Crossline Products";
$grid["sortname"] = 'quantity';
$grid["sortorder"] = "asc";
$grid["viewrecords"] = true;
$grid["autowidth"] = true;
$grid["height"] = "535";
$grid["multiselect"] = false;
$grid["showhidecolumns"] = true;


$g->set_options($grid);


$g->set_actions(array(
    "add" => true,
    "edit" => true,
    "delete" => true,
    "rowactions" => true,
    "search" => "advance"
        )
);

//$e["on_insert"] = array("add_product", null, false);
//$e["on_update"] = array("update_product", null, false);
$e["on_delete"] = array("delete_product", null, false);
$g->set_events($e);

$g->select_command = "SELECT idproduct,name,quantity,price,description,image,timestamp,category FROM product p
                        INNER JOIN category c ON p.category_idcategory = c.idcategory WHERE p.active = true AND c.active=true";

$g->table = "product";

$g->set_columns($cols);

$out = $g->render("list1");
?>
